app.controller("z-emailAddressSet-c", ['$scope', '$rootScope', '$z-http', '$sce', 'z-emailAddressSet-s', 'z-edit-menu-s','$filter',
    function ($scope, $rootScope, $zhttp, $sce, Service, MenuService, $filter) {
        $scope.viewModel = {};
        $scope.editModel = {};
        $scope.viewModel.emailAddress="";
        $scope.editModel.emailAddress="";
		
        //查询页面名称
        MenuService.getMenuByUrl({url: 'emailAddressSet'},
            function (data) {
                $scope.viewModel.menuList = data;
            }, function (error) {
            }
        );

        //加载数据
        $scope.viewModel.bindData = function(){
        	$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getEmail({
				emailAddress:$scope.viewModel.emailAddress
			},
			function(data){
				$scope.viewModel.emailAddress= data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
        };
        $scope.viewModel.bindData();

        //加载
        $scope.viewModel.load=function(){
            $scope.viewModel.show=true;
        };

        //关闭加载
        $scope.viewModel.unLoad=function(){
            $scope.viewModel.show=false;
            $scope.editModel.emailAddress="";
        };

        //修正
        $scope.viewModel.update=function(emailAddress){
            $scope.editModel.title="修改邮箱地址配置";
            $scope.editModel.data=angular.copy(emailAddress);
            $scope.viewModel.load();
        };
        
        //验证
        $scope.viewModel.emailForm= function() {
        	  if (!$scope.editForm.$valid) {
              	if(!$scope.editForm.emailAddress.$dirty){
              		$scope.editForm.emailAddress.$dirty=true;
              	}
              	$rootScope.alertError("您输入的邮箱有误！");
                  return true;
              }else{
              	return false;
              }
        };

        // 保存
        $scope.viewModel.save=function () {
        	if ( $scope.viewModel.emailForm() ) {
                return ;
            }
        	 
            $scope.viewModel.isLoading=true;
                   
             Service.updateEmail({
            			emailAddress:$scope.editModel.emailAddress
            		},
                    function (data) {
            			$scope.editModel.emailAddress=data;
                        $scope.viewModel.bindData();
                        $scope.viewModel.isLoading=false;
                        $rootScope.alertInfo("修改成功");
                        $scope.viewModel.unLoad();
                    },
                    function (error) {
                        $scope.viewModel.isLoading=false;
                        $rootScope.alertError(error.fault.message);
                    }
             ) 
             
        }
        
    }]
);